﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;

namespace AEBaseCalcCSharp
{
    public partial class Form1 : Form
    {
        Base astroBase;

        public Form1()
        {
            InitializeComponent();
            gpbStructures.Enabled = false;
            gpbTechnologies.Enabled = false;
            gpbCommander.Enabled = false;
            cmbAstroList.Items.Add("Arid");
            cmbAstroList.Items.Add("Asteroid");
            cmbAstroList.Items.Add("Craters");
            cmbAstroList.Items.Add("Crystalline");
            cmbAstroList.Items.Add("Earthly");
            cmbAstroList.Items.Add("Gaia");
            cmbAstroList.Items.Add("Glacial");
            cmbAstroList.Items.Add("Magma");
            cmbAstroList.Items.Add("Metallic");
            cmbAstroList.Items.Add("Oceanic");
            cmbAstroList.Items.Add("Radioactive");
            cmbAstroList.Items.Add("Rocky");
            cmbAstroList.Items.Add("Toxic");
            cmbAstroList.Items.Add("Tundra");
            cmbAstroList.Items.Add("Volcanic");
            cmbAstroList.SelectedItem = "Arid";
        }

        private void cmbAstroList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbAstroList.SelectedItem == "Asteroid")
            {
                gpbAstroType.Enabled = false;
            }
            else
            {
                gpbAstroType.Enabled = true;
            }
        }

        private void btnSubmitAstro_Click(object sender, EventArgs e)
        {
            bool successful = true;
            successful = createAstro();

            if (successful == true)
            {
                populateStats();
                populateInitialBaseStats();
                astroBase.incrementLevel("UrbanStructure", 1);
                gpbStructures.Enabled = true;
                gpbTechnologies.Enabled = true;
                gpbCommander.Enabled = true;
            }
        }

        #region Setup Astro
        private bool createAstro()
        {
            string type = "unset";
            int position = 0;

            if (cmbAstroList.SelectedItem != "Asteroid")
            {
                type = getType();
                if (type == "unset")
                {
                    return false;
                }
            }

            position = getPosition();
            if (position == 0)
            {
                return false;
            }

            if (cmbAstroList.SelectedItem == "Arid")
            {
                if (type == "Planet")
                {
                    astroBase = new Base(new AridPlanet(), position);
                }
                else
                {
                    astroBase = new Base(new AridMoon(), position);
                }
            }
            else if (cmbAstroList.SelectedItem == "Asteroid")
            {
                astroBase = new Base(new Asteroid(), position);
            }
            else if (cmbAstroList.SelectedItem == "Craters")
            {
                if (type == "Planet")
                {
                    astroBase = new Base(new CratersPlanet(), position);
                }
                else
                {
                    astroBase = new Base(new CratersMoon(), position);
                }
            }
            else if (cmbAstroList.SelectedItem == "Crystalline")
            {
                if (type == "Planet")
                {
                    astroBase = new Base(new CrystallinePlanet(), position);
                }
                else
                {
                    astroBase = new Base(new CrystallineMoon(), position);
                }
            }
            else if (cmbAstroList.SelectedItem == "Earthly")
            {
                if (type == "Planet")
                {
                    astroBase = new Base(new EarthlyPlanet(), position);
                }
                else
                {
                    astroBase = new Base(new EarthlyMoon(), position);
                }
            }
            else if (cmbAstroList.SelectedItem == "Gaia")
            {
                if (type == "Planet")
                {
                    astroBase = new Base(new GaiaPlanet(), position);
                }
                else
                {
                    astroBase = new Base(new GaiaMoon(), position);
                }
            }
            else if (cmbAstroList.SelectedItem == "Glacial")
            {
                if (type == "Planet")
                {
                    astroBase = new Base(new GlacialPlanet(), position);
                }
                else
                {
                    astroBase = new Base(new GlacialMoon(), position);
                }
            }
            else if (cmbAstroList.SelectedItem == "Magma")
            {
                if (type == "Planet")
                {
                    astroBase = new Base(new MagmaPlanet(), position);
                }
                else
                {
                    astroBase = new Base(new MagmaMoon(), position);
                }
            }
            else if (cmbAstroList.SelectedItem == "Metallic")
            {
                if (type == "Planet")
                {
                    astroBase = new Base(new MetallicPlanet(), position);
                }
                else
                {
                    astroBase = new Base(new MetallicMoon(), position);
                }
            }
            else if (cmbAstroList.SelectedItem == "Oceanic")
            {
                if (type == "Planet")
                {
                    astroBase = new Base(new OceanicPlanet(), position);
                }
                else
                {
                    astroBase = new Base(new OceanicMoon(), position);
                }
            }
            else if (cmbAstroList.SelectedItem == "Radioactive")
            {
                if (type == "Planet")
                {
                    astroBase = new Base(new RadioactivePlanet(), position);
                }
                else
                {
                    astroBase = new Base(new RadioactiveMoon(), position);
                }
            }
            else if (cmbAstroList.SelectedItem == "Rocky")
            {
                if (type == "Planet")
                {
                    astroBase = new Base(new RockyPlanet(), position);
                }
                else
                {
                    astroBase = new Base(new RockyMoon(), position);
                }
            }
            else if (cmbAstroList.SelectedItem == "Toxic")
            {
                if (type == "Planet")
                {
                    astroBase = new Base(new ToxicPlanet(), position);
                }
                else
                {
                    astroBase = new Base(new ToxicMoon(), position);
                }
            }
            else if (cmbAstroList.SelectedItem == "Tundra")
            {
                if (type == "Planet")
                {
                    astroBase = new Base(new TundraPlanet(), position);
                }
                else
                {
                    astroBase = new Base(new TundraMoon(), position);
                }
            }
            else if (cmbAstroList.SelectedItem == "Volcanic")
            {
                if (type == "Planet")
                {
                    astroBase = new Base(new VolcanicPlanet(), position);
                }
                else
                {
                    astroBase = new Base(new VolcanicMoon(), position);
                }
            }

            return true;
        }

        private string getType()
        {
            if (optPlanet.Checked == true)
            {
                return "Planet";
            }
            else
            {
                if (optMoon.Checked == true)
                {
                    return "Moon";
                }
                else
                {
                    MessageBox.Show("You must select either Planet or Moon for this astro type.",
                        "Error: Missing Value",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Exclamation);
                    return "unset";
                }
            }
        }

        private int getPosition()
        {
            if (opt1.Checked == true)
            {
                return 1;
            }
            else
            {
                if (opt2.Checked == true)
                {
                    return 2;
                }
                else
                {
                    if (opt3.Checked == true)
                    {
                        return 3;
                    }
                    else
                    {
                        if (opt4.Checked == true)
                        {
                            return 4;
                        }
                        else
                        {
                            if (opt5.Checked == true)
                            {
                                return 5;
                            }
                            else
                            {
                                MessageBox.Show("You must select a position for the astro.",
                                    "Error: Mising Value",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Exclamation);
                                return 0;
                            }
                        }
                    }
                }
            }
        }

        private void populateStats()
        {
            int solarValue = 5 - astroBase.getPosition();
            lblMetal.Text = astroBase.getMetal().ToString();
            lblSolar.Text = solarValue.ToString();
            lblGas.Text = astroBase.getGas().ToString();
            lblCrystals.Text = astroBase.getCrystals().ToString();
            lblFertility.Text = astroBase.getFertility().ToString();
            lblArea.Text = astroBase.getArea().ToString();
        }

        private void populateInitialBaseStats()
        {
            lblBasePopulation.Text = astroBase.getBaseFertility().ToString();
            lblBaseFertility.Text = astroBase.getBaseFertility().ToString();
            lblBaseEnergy.Text = "3";
            lblBaseArea.Text = astroBase.getCurrentArea().ToString();
        }
        #endregion

        private void btnSubmitValues_Click(object sender, EventArgs e)
        {
            astroBase.incrementLevel("BiosphereModification", Convert.ToInt32(nudBiosphereModification.Value));
            astroBase.incrementLevel("UrbanStructure", Convert.ToInt32(nudUrbanStructure.Value));
            astroBase.incrementLevel("SolarPlants", Convert.ToInt32(nudSolarPlants.Value));
            astroBase.incrementLevel("GasPlants", Convert.ToInt32(nudGasPlants.Value));
            astroBase.incrementLevel("FusionPlants", Convert.ToInt32(nudFusionPlants.Value));
            astroBase.incrementLevel("AntimatterPlants", Convert.ToInt32(nudAntimatterPlants.Value));
            astroBase.incrementLevel("ResearchLabs", Convert.ToInt32(nudResearchLabs.Value));
            astroBase.incrementLevel("MetalRefineries", Convert.ToInt32(nudMetalRefineries.Value));
            astroBase.incrementLevel("CrystalMines", Convert.ToInt32(nudCrystalMines.Value));
            astroBase.incrementLevel("RoboticFactories", Convert.ToInt32(nudRoboticFactories.Value));
            astroBase.incrementLevel("Shipyards", Convert.ToInt32(nudShipyards.Value));
            astroBase.incrementLevel("OrbitalShipyards", Convert.ToInt32(nudOrbitalShipyards.Value));
            astroBase.incrementLevel("Spaceports", Convert.ToInt32(nudSpaceports.Value));
            astroBase.incrementLevel("CommandCentres", Convert.ToInt32(nudCommandCentres.Value));
            astroBase.incrementLevel("NaniteFactories", Convert.ToInt32(nudNaniteFactories.Value));
            astroBase.incrementLevel("AndroidFactories", Convert.ToInt32(nudAndroidFactories.Value));
            astroBase.incrementLevel("EconomicCentres", Convert.ToInt32(nudEconomicCentres.Value));
            astroBase.incrementLevel("Terraform", Convert.ToInt32(nudTerraform.Value));
            astroBase.incrementLevel("MultiLevelPlatforms", Convert.ToInt32(nudMultiLevelPlatforms.Value));
            astroBase.incrementLevel("OrbitalBase", Convert.ToInt32(nudOrbitalBase.Value));
            astroBase.incrementLevel("JumpGate", Convert.ToInt32(nudJumpGate.Value));
            astroBase.incrementLevel("Capital", Convert.ToInt32(nudCapital.Value));

            astroBase.incrementDefenceLevel("Barracks", Convert.ToInt32(nudBarracks.Value));
            astroBase.incrementDefenceLevel("LaserTurrets", Convert.ToInt32(nudLaserTurrets.Value));
            astroBase.incrementDefenceLevel("MissileTurrets", Convert.ToInt32(nudMissileTurrets.Value));
            astroBase.incrementDefenceLevel("PlasmaTurrets", Convert.ToInt32(nudPlasmaTurrets.Value));
            astroBase.incrementDefenceLevel("IonTurrets", Convert.ToInt32(nudIonTurrets.Value));
            astroBase.incrementDefenceLevel("PhotonTurrets", Convert.ToInt32(nudPhotonTurrets.Value));
            astroBase.incrementDefenceLevel("DisruptorTurrets", Convert.ToInt32(nudDisruptorTurrets.Value));
            astroBase.incrementDefenceLevel("DeflectionShields", Convert.ToInt32(nudDeflectionShields.Value));
            astroBase.incrementDefenceLevel("PlanetaryShield", Convert.ToInt32(nudPlanetaryShield.Value));
            astroBase.incrementDefenceLevel("PlanetaryRing", Convert.ToInt32(nudPlanetaryRing.Value));

            lblTotalCost.Text = astroBase.getTotalCost(nudConstruction.Value).ToString("N0", CultureInfo.CurrentCulture);
            lblTotalTime.Text = astroBase.getTotalTime(nudCybernetics.Value, nudConstruction.Value).ToString();
            lblBasePopulation.Text = astroBase.getPopulation().ToString();
            lblBaseFertility.Text = astroBase.getFertility().ToString();
            lblBaseEnergy.Text = astroBase.getEnergy(nudEnergy.Value).ToString();
            lblBaseArea.Text = astroBase.getCurrentArea().ToString();

            lblConstructionCapacity.Text = astroBase.getConstruction(nudCybernetics.Value).ToString();
            lblProductionCapacity.Text = astroBase.getProduction(nudCybernetics.Value);

            checkForNegativeValues();
        }

        private void checkForNegativeValues()
        {
            if (astroBase.getPopulation() < 0)
            {
                lblBasePopulation.BackColor = Color.Red;
            }
            else
            { 
                lblBasePopulation.BackColor = SystemColors.Control;
            }

            if (astroBase.getEnergy(nudEnergy.Value) < 0)
            {
                lblBaseEnergy.BackColor = Color.Red;
            }
            else
            {
                lblBaseEnergy.BackColor = SystemColors.Control;
            }

            if (astroBase.getCurrentArea() < 0)
            {
                lblBaseArea.BackColor = Color.Red;
            }
            else
            {
                lblBaseArea.BackColor = SystemColors.Control;
            }
        }

        private void btnClearValues_Click(object sender, EventArgs e)
        {
            nudUrbanStructure.Value = 1;
            nudSolarPlants.Value = 0;
            nudGasPlants.Value = 0;
            nudFusionPlants.Value = 0;
            nudAntimatterPlants.Value = 0;
            nudResearchLabs.Value = 0;
            nudMetalRefineries.Value = 0;
            nudCrystalMines.Value = 0;
            nudRoboticFactories.Value = 0;
            nudShipyards.Value = 0;
            nudOrbitalShipyards.Value = 0;
            nudSpaceports.Value = 0;
            nudCommandCentres.Value = 0;
            nudNaniteFactories.Value = 0;
            nudAndroidFactories.Value = 0;
            nudEconomicCentres.Value = 0;
            nudTerraform.Value = 0;
            nudMultiLevelPlatforms.Value = 0;
            nudOrbitalBase.Value = 0;
            nudJumpGate.Value = 0;
            nudBiosphereModification.Value = 0;
            nudCapital.Value = 0;
            nudEnergy.Value = 0;
            nudCybernetics.Value = 0;
            nudConstruction.Value = 0;

            nudBarracks.Value = 0;
            nudLaserTurrets.Value = 0;
            nudMissileTurrets.Value = 0;
            nudPlasmaTurrets.Value = 0;
            nudIonTurrets.Value = 0;
            nudPhotonTurrets.Value = 0;
            nudDisruptorTurrets.Value = 0;
            nudDeflectionShields.Value = 0;
            nudPlanetaryShield.Value = 0;
            nudPlanetaryRing.Value = 0;

            lblConstructionCapacity.Text = "12";
            lblProductionCapacity.Text = "0";

            lblTotalCost.Text = "0";
            lblTotalTime.Text = "00:00:00";

            astroBase.resetLevels();
            populateInitialBaseStats();
            checkForNegativeValues();
        }
    }
}